﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Globalization;
using System.Resources;
using Google.GData.YouTube;

using Google.YouTube;
using Google.GData.Extensions.Location;
using Google.GData.Extensions.MediaRss;
using Google.GData.Client;
using System.IO;
using System.Runtime.InteropServices;

namespace WLS
{


    public partial class upload : Form
    {
        [DllImport("coredll.dll")]
        public static extern IntPtr SetCursor(IntPtr hCursor);


        ArrayList video;
        all_setting p;

        public upload(set all, ref all_setting _jazyk)
        {
            InitializeComponent();
            video = (ArrayList)(((set)all).videos);
            p = ((set)all).p;
            p = _jazyk;
            p.JazykZmenen += new all_setting.ZmenaJazyku(jazyk_JazykZmenen);
            jazyk_JazykZmenen(p.jazyk);
        }


        string sum1, sum2, sum3;
        /// <summary>
        /// Změní jazyk
        /// </summary>
        /// <param name="jazyk"></param>
        void jazyk_JazykZmenen(string jazyk)
        {
            CultureInfo cultura = CultureInfo.CreateSpecificCulture(jazyk);
            ResourceManager manager = new ResourceManager("WLS.Res", typeof(Form1).Assembly);
            button1.Text = manager.GetString("uploadonserver", cultura);
            button2.Text = manager.GetString("back", cultura);
            this.Text = manager.GetString("uploadmain", cultura);
            logupload = manager.GetString("logupload", cultura);
            messagebox1 = manager.GetString("messagebox1", cultura);
            messagebox2 = manager.GetString("messagebox2", cultura);
            sum1 = manager.GetString("silver", cultura);
            sum2 = manager.GetString("you", cultura);
            sum3 = manager.GetString("data", cultura);
            label4.Text = manager.GetString("info", cultura);

        }
        string logupload;
        string messagebox1;
        string messagebox2;
        private void button1_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            Cursor.Show();
            if (p.up_messages == true)
            {
                DialogResult result = MessageBox.Show(messagebox1, messagebox2, MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                if (result == DialogResult.Yes)
                {
                    youtubeupload();
                    calculate();
                }
            }
            else
            {
                youtubeupload();
                calculate();
            }
            Cursor.Hide();
            Cursor.Current = Cursors.Default;
        }

        private void youtubeupload()
        {
            for (int i = 0; i < video.Count; i++)
            {
                try
                {
                    //Upload for youtube
                    if (((((data)video[i]).uplaod & 2) == 2) && File.Exists(((data)video[i]).path))
                    {
                        Uri uri = new Uri("http://gdata.youtube.com/feeds/api/users/GoogleDevelopers/uploads");
                        YouTubeRequestSettings settings = new YouTubeRequestSettings("WLS", "WLSProject", "AI39si72pZnkojB6Bb08wGTVs-vogSB1pYbG3zAdfWEvHvWdlHtZ5MNascspkLyw8oukWWNQJyy-GlGarUIUwnJT7YlGk-Q2mQ", p.yt_ac, p.yt_pass);
                        settings.Timeout = 1000000;
                        YouTubeRequest request = new YouTubeRequest(settings);
                        Video newVideo = new Video();
                        newVideo.Title = ((youtube)((data)video[i]).yout).filesetname;
                        switch (((youtube)((data)video[i]).yout).category)
                        {
                            case 0:

                                newVideo.Tags.Add(new MediaCategory("Autos", YouTubeNameTable.CategorySchema));
                                break;
                            case 1:
                                newVideo.Tags.Add(new MediaCategory("Comedy", YouTubeNameTable.CategorySchema));
                                break;
                            case 2:
                                newVideo.Tags.Add(new MediaCategory("Education", YouTubeNameTable.CategorySchema));
                                break;

                            case 3:
                                newVideo.Tags.Add(new MediaCategory("Entertainment", YouTubeNameTable.CategorySchema));
                                break;

                            case 4:
                                newVideo.Tags.Add(new MediaCategory("Film", YouTubeNameTable.CategorySchema));
                                break;

                            case 5:
                                newVideo.Tags.Add(new MediaCategory("Games", YouTubeNameTable.CategorySchema));
                                break;

                            case 6:
                                newVideo.Tags.Add(new MediaCategory("Howto", YouTubeNameTable.CategorySchema));
                                break;

                            case 7:
                                newVideo.Tags.Add(new MediaCategory("Music", YouTubeNameTable.CategorySchema));
                                break;

                            case 8:
                                newVideo.Tags.Add(new MediaCategory("News", YouTubeNameTable.CategorySchema)); //Jde
                                break;

                            case 9:
                                newVideo.Tags.Add(new MediaCategory("Nonprofit", YouTubeNameTable.CategorySchema));
                                break;

                            case 10:
                                newVideo.Tags.Add(new MediaCategory("People", YouTubeNameTable.CategorySchema));
                                break;

                            case 11:
                                newVideo.Tags.Add(new MediaCategory("Animals", YouTubeNameTable.CategorySchema)); // 
                                break;

                            case 12:
                                newVideo.Tags.Add(new MediaCategory("Tech", YouTubeNameTable.CategorySchema));
                                break;

                            case 13:
                                newVideo.Tags.Add(new MediaCategory("Sports", YouTubeNameTable.CategorySchema));
                                break;

                            case 14:
                                newVideo.Tags.Add(new MediaCategory("Travel", YouTubeNameTable.CategorySchema));
                                break;
                        }


                        newVideo.Keywords = ((youtube)((data)video[i]).yout).keywords;
                        newVideo.Description = ((youtube)((data)video[i]).yout).summary;
                        if (((youtube)((data)video[i]).yout).type == true)
                            newVideo.YouTubeEntry.Private = false;
                        else
                            newVideo.YouTubeEntry.Private = true;
                        newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);
                        newVideo.YouTubeEntry.MediaSource = new MediaFileSource(((data)video[i]).path, "video/quicktime");
                        Video createdVideo = request.Upload(newVideo);
                        int vyber = ((data)video[i]).uplaod;
                        ((data)video[i]).uplaod -= 2;
                        if (((data)video[i]).uplaod == 0 && p.delete_video == true)
                        {
                            video.RemoveAt(i);
                            i--;
                        }
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                    p.log = "Error: " + ex.Message + Environment.NewLine + p.log;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void upload_Load(object sender, EventArgs e)
        {

            if (this.Tag != null)
            {
                p = (all_setting)((set)this.Tag).p;
                video = (ArrayList)((set)this.Tag).videos;
            }

            calculate();
        }

        private void calculate()
        {
            double size = 0;
            string size_text;

            int you = 0;
            int silver = 0;
            foreach (data v in video)
            {
                byte vyber = v.uplaod;

                if ((vyber & 3) == 3)
                {
                    size += Convert.ToDouble(v.velikost) * 2;
                    you++;
                    silver++;
                }
                else if (((vyber & 2) == 2))
                {
                    size += Convert.ToDouble(v.velikost);
                    you++;
                }
                else if (((vyber & 1) == 1))
                {
                    size += Convert.ToDouble(v.velikost);
                    silver++;
                }
            }

            if (size < 1048576) { size = size / 1024; size_text = string.Format("{0:N2} {1}", size, "KB"); } else { size = size / 1048576; size_text = string.Format("{0:N2} {1}", size, "MB"); }
            label5.Text = silver.ToString();
            label6.Text = you.ToString();
            label7.Text = size_text;
        }
    }
}